<?php
	require_once('config.inc.php');
	
	
	if (isset($_POST['update_valid_time_slots']))
	{
		$classroom_id = mysql_real_escape_string(strip_tags($_POST['current_classroom']));
		$instructor_id = mysql_real_escape_string(strip_tags($_POST['current_instructor']));
		$building_id = mysql_real_escape_string(strip_tags($_POST['current_building']));
		$semester_id = mysql_real_escape_string(strip_tags($_POST['current_semester']));
		$course_number = mysql_real_escape_string(strip_tags($_POST['course_number']));
		$current_scheduled_classroom = mysql_real_escape_string(strip_tags($_POST['current_scheduled_classroom']));
		$current_scheduled_timeslot = mysql_real_escape_string(strip_tags($_POST['current_scheduled_timeslot']));
		
		$availQuery = "SELECT *
					   FROM instructor_availability ia
					   JOIN days ON days.id = ia.day_id
					   WHERE instructor_id = '$instructor_id'
					   ORDER by day_id";
		$availResult = mysql_query($availQuery);
		//echo mysql_error();
		$count = 0;
		$last_day_id = 0;
		while ($availRow = mysql_fetch_assoc($availResult))
		{
			if ($availRow['day_id'] != $last_day_id)
			{
				$count = 0;
			}
			$count++;
			
			$day_name = $availRow['day_name'];
			$start_time = $availRow['start_time'];
			$end_time = $availRow['end_time'];
			
			$availability[$day_name][$count]['start_time'] = $start_time;
			$availability[$day_name][$count]['end_time'] = $end_time;
			
			$last_day_id = $availRow['day_id'];
		}
		
		$timeslotQuery = "SELECT *
						  FROM time_slots
						  WHERE building_id = '$building_id'";
		
		$timeslotResult = mysql_query($timeslotQuery);
		$valid_timeslots = array();
		
		/*loop through each timelsot*/
		while($timeslotRow = mysql_fetch_assoc($timeslotResult))
		{
			$ts_start_time = $timeslotRow['start_time'];
			$ts_end_time = $timeslotRow['end_time'];
			$Monday = $timeslotRow['Monday'];
			$Tuesday = $timeslotRow['Tuesday'];
			$Wednesday = $timeslotRow['Wednesday'];
			$Thursday = $timeslotRow['Thursday'];
			$Friday = $timeslotRow['Friday'];
			$Saturday = $timeslotRow['Saturday'];
			$Sunday = $timeslotRow['Sunday'];
			$ts_days = array('Monday' => $Monday, 'Tuesday' => $Tuesday, 'Wednesday' => $Wednesday, 'Thursday' => $Thursday, 'Friday' => $Friday, 'Saturday' => $Saturday, 'Sunday' => $Sunday);
			
			$valid_timeslot = TRUE;
			
			/*go through each day of the week, checking to see if the timeslot falls within
			 the instructor's availability for that day and checking to ensure the timeslot
			 does not overlap with a course that's already scheduled for that instructor*/
			foreach ($ts_days as $day => $val)
			{
				$valid_for_this_day = TRUE;
				if ($val == '1')
				{
					$valid_for_this_day = FALSE;
					foreach ($availability[$day] as $avail_slot)
					{
						if ($ts_start_time >= $avail_slot['start_time'] && $ts_end_time <= $avail_slot['end_time'])
						{
							$valid_for_this_day = TRUE;
						}
					}
					
					//check if the instructor is already scheduled for a time that overlaps this timeslot
					if ($instructor_id != '1')
					{
						$alreadyScheduledQuery = "SELECT *
												  FROM schedule 
												  JOIN course_sections cs ON cs.id = schedule.course_section_id
												  JOIN time_slots ts ON ts.id = schedule.time_slot_id
												  WHERE instructor_id = '$instructor_id'
												  AND semester_id = '$semester_id'
												  AND ts.id != '$current_scheduled_timeslot'";
						$alreadyScheduledResult = mysql_query($alreadyScheduledQuery);
						while ($asRow = mysql_fetch_assoc($alreadyScheduledResult))
						{
							if ($asRow[$day] == 1)
							{
								if (($asRow['start_time'] < $ts_start_time && $asRow['end_time'] > $ts_start_time) || ($asRow['start_time'] < $ts_end_time && $asRow['end_time'] > $ts_end_time) || ($asRow['start_time'] <= $ts_start_time && $asRow['end_time'] >= $ts_end_time) || ($asRow['start_time'] >= $ts_start_time && $asRow['end_time'] <= $ts_end_time))
								{
									$valid_for_this_day = FALSE;
								}
							}
						}
					}
					
					//check to see if the course has a lab.
					//if it does have a lab, check that this timeslot does not overlap
					// with an associated, already scheduled course
					$labQuery = "SELECT *
								 FROM schedule s
								 JOIN time_slots ts ON ts.id = s.time_slot_id
								 JOIN course_sections cs ON cs.id = s.course_section_id
								 JOIN course_section_mapping csm ON csm.course_section_id = cs.id
								 JOIN courses c ON c.id = csm.course_id
								 WHERE c.id IN (SELECT c1.id 
												   FROM courses c1
												   JOIN courses c2 ON c2.lab_number = c1.id
												   WHERE c2.course_number = '$course_number')
								 AND ts.id != '$current_scheduled_timeslot'";
					$labResult = mysql_query($labQuery);
					while ($labRow = mysql_fetch_assoc($alreadyScheduledResult))
					{
						if ($labRow[$day] == 1)
						{
							if (($labRow['start_time'] < $ts_start_time && $labRow['end_time'] > $ts_start_time) || ($labRow['start_time'] < $ts_end_time && $labRow['end_time'] > $ts_end_time) || ($labRow['start_time'] <= $ts_start_time && $labRow['end_time'] >= $ts_end_time) || ($labRow['start_time'] >= $ts_start_time && $labRow['end_time'] <= $ts_end_time))
							{
								$valid_for_this_day = FALSE;
							}
						}
					}
					
					//check to see if the course IS a lab.
					//if it is a lab, check that this timeslot does not overlap
					// with an associated, already scheduled course
					$labQuery = "SELECT *
								 FROM schedule s
								 JOIN time_slots ts ON ts.id = s.time_slot_id
								 JOIN course_sections cs ON cs.id = s.course_section_id
								 JOIN course_section_mapping csm ON csm.course_section_id = cs.id
								 JOIN courses c ON c.id = csm.course_id
								 WHERE c.lab_number IN (SELECT c1.lab_number 
														FROM courses c1
														JOIN courses c2 ON c1.lab_number = c2.id
														WHERE c2.course_number = '$course_number')
								 AND ts.id != '$current_scheduled_timeslot'";
					$labResult = mysql_query($labQuery);
					while ($labRow = mysql_fetch_assoc($alreadyScheduledResult))
					{
						if ($labRow[$day] == 1)
						{
							if (($labRow['start_time'] < $ts_start_time && $labRow['end_time'] > $ts_start_time) || ($labRow['start_time'] < $ts_end_time && $labRow['end_time'] > $ts_end_time) || ($labRow['start_time'] <= $ts_start_time && $labRow['end_time'] >= $ts_end_time) || ($labRow['start_time'] >= $ts_start_time && $labRow['end_time'] <= $ts_end_time))
							{
								$valid_for_this_day = FALSE;
							}
						}
					}
					
					$scheduledCoursesQuery = "SELECT * FROM schedule sch
											  JOIN time_slots ts ON ts.id = sch.time_slot_id
											  WHERE classroom_id = '$classroom_id'
											  AND ts.id != '$current_scheduled_timeslot'";
					$scheduledCoursesResult = mysql_query($scheduledCoursesQuery);
					while ($scRow = mysql_fetch_assoc($scheduledCoursesResult))
					{
						$sc_start_time = $scRow['start_time'];
						$sc_end_time = $scRow['end_time'];
						$sc_Monday = $scRow['Monday'];
						$sc_Tuesday = $scRow['Tuesday'];
						$sc_Wednesday = $scRow['Wednesday'];
						$sc_Thursday = $scRow['Thursday'];
						$sc_Friday = $scRow['Friday'];
						$sc_Saturday = $scRow['Saturday'];
						$sc_Sunday = $scRow['Sunday'];
						
						/* if the timelsot we're looking at and the scheduled course's timeslot are both
						scheduled on the same day, check to see if the times overlap */
						if ($scRow[$day]  == '1')
						{
							if (($sc_start_time < $ts_start_time && $sc_end_time > $ts_start_time) || ($sc_start_time < $ts_end_time && $sc_end_time > $ts_end_time) || ($sc_start_time <= $ts_start_time && $sc_end_time >= $ts_end_time) || ($sc_start_time >= $ts_start_time && $sc_end_time <= $ts_end_time))
							{
								$valid_for_this_day = FALSE;
							}
						}
					}
					
					
					if (!$valid_for_this_day)
					{
						$valid_timeslot = FALSE;
					}
				}
			}
			
			/*If the timeslot fits the instructor's schedule and doesn't overlap with an already
			scheduled sections, push it on the array of valid timeslots for this section and instructor*/
			if ($valid_timeslot)
			{
				array_push($valid_timeslots, $timeslotRow['id']);	
				//echo $timeslotRow['id'].', ';
			}
		}
		
		$timeslot_list = '';
		foreach ($valid_timeslots as $ts)
		{
			$timeslot_list .= $ts.',';
		}
		if (preg_match('/,/', $timeslot_list) == 1)
		{
			$timeslot_list = substr_replace($timeslot_list, '', -1, 1);
			echo $timeslot_list;
		}
		else
		{
			echo 'false';
		}
	}
	
	
	
	
	
	if (isset($_POST['update_valid_classrooms']))
	{
		$timeslot_id = mysql_real_escape_string(strip_tags($_POST['current_timeslot']));
		$instructor_id = mysql_real_escape_string(strip_tags($_POST['current_instructor']));
		$building_id = mysql_real_escape_string(strip_tags($_POST['current_building']));
		$semester_id = mysql_real_escape_string(strip_tags($_POST['current_semester']));
		$num_seats = mysql_real_escape_string(strip_tags($_POST['cs_num_seats']));
		$current_scheduled_classroom = mysql_real_escape_string(strip_tags($_POST['current_scheduled_classroom']));
		$current_scheduled_timeslot = mysql_real_escape_string(strip_tags($_POST['current_scheduled_timeslot']));
		
		
		$tsQuery = "SELECT *
					FROM time_slots
					WHERE id = '$timeslot_id'";
		$tsResult = mysql_query($tsQuery);
		$tsRow = mysql_fetch_assoc($tsResult);
		$ts_start_time = $tsRow['start_time'];
		$ts_end_time = $tsRow['end_time'];
		$ts_Monday = $tsRow['Monday'];
		$ts_Tuesday = $tsRow['Tuesday'];
		$ts_Wednesday = $tsRow['Wednesday'];
		$ts_Thursday = $tsRow['Thursday'];
		$ts_Friday = $tsRow['Friday'];
		$ts_Saturday = $tsRow['Saturday'];
		$ts_Sunday = $tsRow['Sunday'];
		
		/*get all classrooms in the building that have enough seats for the section*/
		$classroomQuery = "SELECT id
						   FROM classrooms 
						   WHERE building_id = '$building_id'
						   AND num_seats >= '$num_seats'
						   ORDER BY num_seats ASC";
		$classroomResult = mysql_query($classroomQuery);
		
		$valid_classrooms = array();
		
		while ($crRow = mysql_fetch_assoc($classroomResult))
		{
			$cReason = '';
			$scToken = FALSE;
			$ccToken = FALSE;
			
			$classroom_id = $crRow['id'];
			$classroom_available = TRUE;
			
			$scheduledCoursesQuery = "SELECT * FROM schedule sch
									  JOIN time_slots ts ON ts.id = sch.time_slot_id
									  WHERE classroom_id = '$classroom_id'
									  AND classroom_id != '$current_scheduled_classroom'";
			$scheduledCoursesResult = mysql_query($scheduledCoursesQuery);
			while ($scRow = mysql_fetch_assoc($scheduledCoursesResult))
			{
				$sc_start_time = $scRow['start_time'];
				$sc_end_time = $scRow['end_time'];
				$sc_Monday = $scRow['Monday'];
				$sc_Tuesday = $scRow['Tuesday'];
				$sc_Wednesday = $scRow['Wednesday'];
				$sc_Thursday = $scRow['Thursday'];
				$sc_Friday = $scRow['Friday'];
				$sc_Saturday = $scRow['Saturday'];
				$sc_Sunday = $scRow['Sunday'];
				
				/* if the timelsot we're looking at and the scheduled course's timeslot are both
				scheduled on the same day, check to see if the times overlap */
				if (($ts_Monday == 1 && $sc_Monday == 1) || ($ts_Tuesday == 1 && $sc_Tuesday == 1) || ($ts_Wednesday == 1 && $sc_Wednesday == 1) || ($ts_Thursday == 1 && $sc_Thursday == 1) || ($ts_Friday == 1 && $sc_Friday == 1) || ($ts_Saturday == 1 && $sc_Saturday == 1) || ($ts_Sunday == 1 && $sc_Sunday == 1))
				{
					if (($sc_start_time < $ts_start_time && $sc_end_time > $ts_start_time) || ($sc_start_time < $ts_end_time && $sc_end_time > $ts_end_time) || ($sc_start_time <= $ts_start_time && $sc_end_time >= $ts_end_time) || ($sc_start_time >= $ts_start_time && $sc_end_time <= $ts_end_time))
					{
						$classroom_available = FALSE;
					}
				}
			}
			
			/*Check for classroom closings*/
			$closingQuery = "SELECT *
							 FROM classroom_closings cc
							 JOIN days on days.id = cc.day_id
							 WHERE classroom_id = '$classroom_id'
							 AND classroom_id != '$current_scheduled_classroom'";
			$closingResult = mysql_query($closingQuery);
			while ($closingRow = mysql_fetch_assoc($closingResult))
			{
				$c_day_name = $closingRow['day_name'];
				$c_start_time = $closingRow['start_time'];
				$c_end_time = $closingRow['end_time'];
				
				if (${'ts_'.$c_day_name} == 1)
				{
					if (($c_start_time < $ts_start_time && $c_end_time > $ts_start_time) || ($c_start_time < $ts_end_time && $c_end_time > $ts_end_time) || ($c_start_time <= $ts_start_time && $c_end_time >= $ts_end_time) || ($c_start_time >= $ts_start_time && $c_end_time <= $ts_end_time))
					{
						$classroom_available = FALSE;
					}
				}
			}
			
			
			if ($classroom_available)
			{
				array_push($valid_classrooms, $classroom_id);
			}
		}
		
		$classroom_list = '';
		foreach ($valid_classrooms as $cr)
		{
			$classroom_list .= $cr.',';
		}
		if (preg_match('/,/', $classroom_list) == 1)
		{
			$classroom_list = substr_replace($classroom_list, '', -1, 1);
			echo $classroom_list;
		}
		else
		{
			echo 'false';
		}
	}
	
?>